package com.bicap.cloud.nettyserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bicap.cloud.nettyserver.config.NettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/14 0014.
 */
@RestController
public class TestController {

    @Autowired
    private NettyServer nettyServer;


    @RequestMapping("/send2client")
    public String sendMsg2Client(String msg){

        Map<String,Object> map = new HashMap<>();
        String ip = "10.186.106.242";
        map.put("subjectId",5);
        map.put("msg",msg);
        map.put("ipAddr",ip);
        String json = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(json);
        map.remove("ipAddr");
        String json2 = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(json2);

        nettyServer.getServerDataHandler().getChanelMap().get(ip).writeAndFlush(json);
        return "send to client success";
    }
}
